package com.xingkeduo.controller.cms.marketing;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.MarketingBaseController;
import com.xingkeduo.model.param.MarketingParam;
import com.xingkeduo.model.param.MarketingParam.MarketingCouponStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingReceiveTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingServiceTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.service.cms.marketing.MarketingTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: ManagementCouponController
 * @Description: 市场营销管理-券处理相关
 * @author lixin
 * @date Apr 18, 2019 9:11:07 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/coupon")
public class CouponTemplateCmsController extends MarketingBaseController {
	Logger logger = LoggerFactory.getLogger(CouponTemplateCmsController.class);
	@Autowired
	@Qualifier(value = "marketingCouponTemplateService")
	MarketingTemplateService marketingCouponTemplateService;

	@RequestMapping("/addCouponTemplate")
	public ResponseMessage addCouponTemplate(@RequestBody MarketingParam couponTemplate) {
		couponTemplate.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		couponTemplate.setStatus(MarketingStatusEnum.DOWN.getCode());
		try {
			this.initParams(couponTemplate);
			return marketingCouponTemplateService.saveOrUpdateCouponByTemplate(couponTemplate);
		} catch (Exception e) {
			logger.error("【添加券异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getCouponTemplateDetail")
	public ResponseMessage getCouponTemplateDetail(Long couponTemplateId) {
		if (couponTemplateId == null) {
			return ResponseMessage.error("券id不合法", 500);
		}
		return marketingCouponTemplateService.queryCouponById(couponTemplateId);
	}

	@RequestMapping("/updateCouponTemplate")
	public ResponseMessage updateCouponTemplate(@RequestBody MarketingParam couponTemplate) {
		couponTemplate.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		try {
			this.initParams(couponTemplate);
			return marketingCouponTemplateService.saveOrUpdateCouponByTemplate(couponTemplate);
		} catch (Exception e) {
			logger.error("【修改券异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/auditCouponTemplate")
	public ResponseMessage auditCouponTemplate(Long couponTemplateId, String status) {
		if (couponTemplateId == null) {
			return ResponseMessage.error("券id不合法", 500);
		}
		if (MarketingStatusEnum.parseStatus(Integer.parseInt(status)).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券状态不合法", 500);
		}
		MarketingParam couponTemplate = new MarketingParam();
		couponTemplate.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		couponTemplate.setId(couponTemplateId);
		couponTemplate.setStatus(Integer.parseInt(status));
		this.initUserInParams(couponTemplate);
		return marketingCouponTemplateService.updateCouponStatusById(couponTemplate);
	}

	@RequestMapping("/getCouponTemplateList")
	public ResponseMessage getCouponTemplateList(MarketingParam couponTemplateExample) {
		couponTemplateExample.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		// 券类型
		if (StringUtils.isNotBlank(couponTemplateExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(couponTemplateExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券类型不合法", 500);
		}
		// 券状态
		if (StringUtils.isNotBlank(couponTemplateExample.getConponStatus()) && MarketingCouponStatusEnum
				.parse(couponTemplateExample.getConponStatus()).equals(MarketingCouponStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券状态不合法", 500);
		}
		// 领取范围
		if (couponTemplateExample.getReceiveType() != null && MarketingReceiveTypeEnum
				.parseStatus(couponTemplateExample.getReceiveType()).equals(MarketingReceiveTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券领取范围不合法", 500);
		}
		// 上下架
		if (couponTemplateExample.getStatus() != null && MarketingStatusEnum
				.parseStatus(couponTemplateExample.getStatus()).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券上下架状态不合法", 500);
		}
		return marketingCouponTemplateService.queryCouponByTemplate(couponTemplateExample);
	}

	@RequestMapping("/getAllCouponTemplateList")
	public ResponseMessage getAllCouponTemplateList(MarketingParam couponTemplateExample) {
		couponTemplateExample.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		return marketingCouponTemplateService.getAllCouponTemplateList(couponTemplateExample);
	}
}
